package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.product.model.BaseCategoryView;
import com.atguigu.gmall.product.service.BaseCategoryViewService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.json.JSONException;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    @Override
//    @GmallCache(prefix = "baseCategoryList")
    public List<JSONObject> getBaseCategoryList() {
        //构建所有以及分类的集合对象
        List<JSONObject> resultList = new ArrayList<JSONObject>();
        //查询分类视图获取所有的分类对象集合
        List<BaseCategoryView> categoryViewList = this.list();
        //对所有对象分类集合进行分组:根据一级id进行分组
        Map<Long, List<BaseCategoryView>> category1ListMap =
                categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        final int[] index = {1};
        Set<Map.Entry<Long, List<BaseCategoryView>>> category1Entries = category1ListMap.entrySet();
        //存放一级
        category1Entries.stream().forEach(categoryView -> {
            JSONObject jsonObject = new JSONObject();
            Long categoryId = categoryView.getKey();
            String categoryName = categoryView.getValue().get(0).getCategory1Name();
            jsonObject.put("categoryId", categoryId);
            jsonObject.put("categoryName", categoryName);
            jsonObject.put("index", index[0]++);
//            resultList.add(jsonObject);
            List<JSONObject> categoryChild = new ArrayList<>();
            //循环遍历二级分类
            List<BaseCategoryView> baseCategory2Views = categoryView.getValue();
            Map<Long, List<BaseCategoryView>> category2ListMap = baseCategory2Views.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> entry : category2ListMap.entrySet()) {
                JSONObject jsonObject1 = new JSONObject();
                Long category2Id = entry.getKey();
                String category2Name = entry.getValue().get(0).getCategory2Name();
                jsonObject1.put("categoryId", category2Id);
                jsonObject1.put("categoryName", category2Name);
                //创建2需要的list容器
                List <JSONObject> categoryChild3 =new ArrayList<>();
                //循环遍历三级分类
                List<BaseCategoryView> BaseCategoryView3 = entry.getValue();
                for (BaseCategoryView baseCategoryView : BaseCategoryView3) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
                    jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
                    categoryChild3.add(jsonObject3);
                }
                //将三级分类的数据进行存储
                jsonObject1.put("categoryChild",categoryChild3);

                categoryChild.add(jsonObject1);
            }
            jsonObject.put("categoryChild", categoryChild);
            resultList.add(jsonObject);
        });
        return resultList;
    }
}
